﻿import QtQuick 2.12
import QtCharts 2.3
import HDataQuery 1.0
import QtQuick.Controls 2.12

//简单的线段描点

ChartView {
    id: chartView
    theme: ChartView.ChartThemeLight
    antialiasing: true
    title: "合格率折线"
    titleColor: "white"
    legend.labelColor:"white"
    legend.markerShape:Legend.MarkerShapeRectangle
    legend.alignment: Qt.AlignBottom

    ValueAxis {
        id:axisX
        min:1
        max:5
        labelsColor: "#13B2E8"
        gridLineColor: "#13B2E8"
        color: "#13B2E8"
    }
    ValueAxis {
        id:axisY
        min:90
        max:101
        labelsColor: "#13B2E8"
        gridLineColor: "#13B2E8"
        color: "#13B2E8"
    }

    ToolTip {
        id: toolTipPop
        parent: chartView
        background: Rectangle {
            color: "#393232"
            radius: 5
        }
        contentItem: Text {
            text: toolTipPop.text
            color: "white"
            font.pixelSize: 15
            padding: 5
            lineHeight: 1.4
        }
    }

    Component.onCompleted: {
        drawline("KeZhou");
        drawline("AKeSu");
        drawline("HaMiJunXin");
    }

    function phraseJson(plantName) {
        let qua = query.getQua(plantName);
        return JSON.parse(qua);
    }
    function drawline(plantName) {
        let quaObj = phraseJson(plantName);
        let line = chartView.createSeries(ChartView.SeriesTypeLine, modelH.trPlantName(plantName), axisX, axisY);
        let linePoints = chartView.createSeries(ChartView.SeriesTypeScatter, modelH.trPlantName(plantName), axisX, axisY);
        for (var prop in quaObj) {
            line.append(quaObj[prop].month, quaObj[prop].qua);
            linePoints.append(quaObj[prop].month, quaObj[prop].qua);
        }
        //   line.pointLabelsVisible = true;
        //   line.pointLabelsClipping = false;
        line.pointsVisible = true;
        line.width = 3;
        linePoints.color = line.color;
        linePoints.markerSize = 10;
        linePoints.hovered.connect(hoverInfo);
    }

    function hoverInfo(point, state) {
        toolTipPop.visible = state;
        if (state) {
            let pos = chartView.mapToPosition(point, chartView.series("KeZhou"));
            toolTipPop.x = pos.x + 10;
            toolTipPop.y = pos.y - toolTipPop.height / 2;
            toolTipPop.text = "横坐标%1\n".arg(point.x) + "纵坐标%1".arg(point.y);
        }
    }
}
